<?php

namespace Common\Model;

/**
 * 支付平台支付信息记录
 * @modify trainyao 2016-05-16 创建
 * @index isdelete_type_hotel(`isdelete`, `type`, `hotelentity_id`)
 */
Class OrderPayInfoModel extends BaseModel {

    protected $trueTableName = 'tb_order_pay_info';
    protected $field = 'id,type,hotelentity_id,orderroom_id,paymethod,wechatpayapp,tradenumber,outtradenumber,buyer,buyerid,payfee,deposit,paytime,paybank,issubscribe,tradetype,more,createtime,updatetime,operator,ischeck,isdelete';

    protected function _before_insert(&$data, $options) {
        $data['operator'] = 0;
    }

    /**
     * 根据客栈返回信息
     * @modify trainyao 2016-05-18 创建
     * @param  integer   $hotelId   客栈ID
     * @return array              信息数组
     */
    public function getInfoByHotelType($hotelId, $type = array()) {
        $option = array(
            'isdelete' => NOT_DELETE,
        );
        if (is_array($type)) {
            $option['type'] = array('in', $type);
        }
        $option['hotelentity_id'] = $hotelId;

        $data = $this->where($option)->field($this->field)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据子订单id与支付方式获取数据
     * @modify trainyao 2016-05-16    创建
     * @modify trainyao 2016-06-22   增加查多条订单代收记录
     * @param mixin $orderRoomId  子订单id(s)
     * @param integer $method   支付方式
     * @return array   信息数组,找不到返回空数组
     */
    public function getInfoByOrderMethod($orderRoomId, $method = null) {
        $option = array(
            'isdelete' => NOT_DELETE,
        );
        if (!is_null($method)) {
            $option['paymethod'] = $method;
        }
        if (is_array($orderRoomId)) {
            $orderRoomId = implode(',', $orderRoomId);
        }
        if (strpos($orderRoomId, ',') === false) {
            $option['orderroom_id'] = $orderRoomId;
            $data = $this->where($option)->field($this->field)->find();
        } else {
            $option['orderroom_id'] = array('in', $orderRoomId);
            $data = $this->where($option)->field($this->field)->select();
        }
        
        return empty($data) ? array() : $data;
    }

    /**
     * 建立支付记录
     * @modify trainyao 2016-05-16      创建
     * @param  integer   $hotelId    客栈ID
     * @param  integer   $orderRoomId    对应子订单ID
     * @param  integer   $method         支付方式
     * @param  string   $tradeNumber    第三方平台订单号
     * @param  string   $outTradeNumber 美住订单号
     * @param  string   $buyer          买家
     * @param  string   $buyerId        买家第三方平台ID
     * @param  string   $payfee         支付金额
     * @param  string   $paytime        支付时间
     * @param  string   $paybank        支付银行
     * @param  integer   $isSubscribe    是否关注微信公众号,扩展使用
     * @param  string   $tradeType      支付类型,扩展使用
     * @param  string   $more           更多信息,200字内
     * @param  integer   $type           支付记录类型 0=畅联代收 1=商家自收
     * @param  integer   $wechatPayApp   0=不属于微信支付 1=钥匙包app起调微信支付 2=微信app起调微信支付
     */
    public function addInfo($hotelId, $orderRoomId, $method, $tradeNumber = null, $outTradeNumber = null, $buyer = null, $buyerId = null, $payfee = null, $paytime = null, $paybank = null, $isSubscribe = null, $tradeType = null, $more = null, $type = 0, $wechatPayApp = null) {
        $data = array(
            'hotelentity_id' => $hotelId,
            'orderroom_id' => $orderRoomId,
            'paymethod' => $method,
            'type' => $type,
        );
        if (!is_null($tradeNumber)) {
            $data['tradenumber'] = $tradeNumber;
        }
        if (!is_null($outTradeNumber)) {
            $data['outtradenumber'] = $outTradeNumber;
        }
        if (!is_null($buyer)) {
            $data['buyer'] = $buyer;
        }
        if (!is_null($buyerId)) {
            $data['buyerid'] = $buyerId;
        }
        if (!is_null($payfee)) {
            $data['payfee'] = $payfee;
        }
        if (!is_null($paytime)) {
            $data['paytime'] = $paytime;
        }
        if (!is_null($paybank)) {
            $data['paybank'] = $paybank;
        }
        if (!is_null($isSubscribe)) {
            $data['issubscribe'] = $isSubscribe;
        }
        if (!is_null($tradeType)) {
            $data['tradetype'] = $tradeType;
        }
        if (!is_null($more)) {
            $data['more'] = $more;
        }
        if (!is_null($wechatPayApp)) {
            $data['wechatpayapp'] = $wechatPayApp;
        }

        return empty($this->add($data)) ? false : true;
    }

    /**
     * 保存信息
     * @modify trainyao 2016-06-21 创建
     * @param  integer   $id        条目ID
     * @param  string   $deposit   订单退款金额
     * @return boolean              执行结果
     */
    public function saveInfo($id, $deposit = null) {
        $option = array(
            'id' => $id,
            'isdelete' => NOT_DELETE,
        );
        $data = array();
        if (!is_null($deposit)) {
            $data['deposit'] = $deposit;
        }

        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }
}
